
/****************************************************************************
**
** Copyright (C) 2004-2008 VATATA.com . All rights reserved.
**
** This file is the part of the Miniutil Poject of VATATA.com.
** Vatata's miniutil library is lightweight and portable (for linux
** and Windows). The C++ Class Library included Threadpool, Dll danymic loading, 
** General socket application model and tcp server, httpserver/servlet, etc. 
** It is a pratical utility disigned for who don't want learn the Big C++ 
** Developing Platform, such as  Boost or ACE.
**
** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by VATATA.com.
** (or its successors, if any) and the Miniutil Project Aministrators. 
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://www.gnu.org/licenses/lgpl.html. If you are unsure which 
** license is appropriate for your use, please review the following 
** information: http://code.google.com/p/miniutil/ or contact 
** http://www.vatata.com.
**
** In addition, as a special exception, VATATA.com, as the sole
** copyright holder for Miniutil Project, grants users of VATATA
** P2P Platform the right to release and package with the related 
** libraries without the source code.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. vatata reserves all rights not expressly
** granted herein.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/



#include "miniutil/pattern.h"

#include "minitest/testcase.h"

#include <iostream>


class testpattern : public testcase
{

public: 
	testpattern()
	{
		themain.tests.push_back( this );
	}

	virtual void test()
	{
		cout << "test pattern" << endl;

		miniutil::pattern * p = miniutil::pattern::compile("(a+)");
		miniutil::matcher * m = p->getMatcher("aa-aaaaa+a");

		cout << "test find ... \t\t" ;
		if ( m->find() >=  0 )
		{
			cout << "OK"<< endl;
		}
		else
			cout << "Fail" << endl;

		cout << "test replace... \t\t ";
		string result;
		if ( m->replace( result, "bc" ) >= 0  )
		{
			cout << "OK\t:" << result << endl;
		}
		else 
			cout << "Fail" << endl;

		cout << "test replaceAll ";
		
		if ( m->replaceAll( result, "b$1c" ) > 0 )
		{
			cout << "OK\t:" << result << endl;
		}
		else 
			cout << "Fail" << endl;

		miniutil::pattern * p2 = miniutil::pattern::compile("([A-Z])", "i");
		cout << "test pattern replace ... \t\t";
		if ( p2->replace( "addd-cddd-eeer", "{$1}", result ) >= 0 )
			cout << "OK\t:\t" << result << endl;
		else
			cout << "Fail"<<endl;

		cout << "test pattern replaceAll ... \t\t";
		if ( p2->replaceAll( "addd-cddd-eeer", "{$1}", result) > 0  )
			cout << "OK\t:\t" << result << endl;
		else
			cout << "Fail"<<endl;
	
		miniutil::pattern * p3 = miniutil::pattern::compile("[^A-Z]", "i");
		vector<string> list;
		cout << "test split ...\t\t";
		if ( p3->split( "addd-ccc-eeeeer", list ) > 0 )
			cout << "OK\t\t:" << list.size() << "\t" << list[ list.size()-1] << endl;
		else
			cout << "Fail"<<endl;
	}
};

testcase * testpattern1 = new testpattern();
